import{modal7,msg,tips} from './layer';
 const editPwd = () => {


     let markup = `
    <form action="" class="form-horizontal modal-form" role="form" id="form">

        <div class="form-group">
            <label for="pwd" class="col-sm-2 control-label">密码:</label>
            <div class="col-sm-9">
                <input type="password" id="pwd" name="pwd"  class="form-control">
            </div>
        </div>
            <div class="form-group">
            <label for="newpwd" class="col-sm-2 control-label">新密码:</label>
            <div class="col-sm-9">
                <input type="password" id="newpwd"   name="newpwd"  class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label for="confirmpwd" class="col-sm-2 control-label">重复新密码:</label>
            <div class="col-sm-9">
                <input type="password" id="confirmpwd"  name="confirmpwd"  class="form-control">
            </div>
        </div>
        <div class="form-group">
            <div class="btn-box">
                <input type="button" class="btn btn-primary" id="submitBtn" value="提交">
            </div>
        </div>
    </form>
    `;

     modal7('修改密码', markup);

     $('#submitBtn').click(() => {
         let data = {};
         $.each($('#form').serializeArray(), (key, val) => {
             data[val.name] = val.value;
         });

         if (!validate(data)) {
             return;
         }

         submit(GLOBAL_pwdUrl, data);
     });

 };


 const loginOut = () => {

     $.post(GLOBAL_logOutUrl, function (res) {
         if (res.rel == 1) {
             msg(res.msg, function () {
                     window.location.href=GLOBAL_loginUrl;
             });
         } else {
             msg(res.msg);
             $('#submitBtn').removeAttr('disabled');
         }
     }, 'json');

 };


 const validate = (data) => {
     if (data.pwd === '') {
         tips('旧密码不能为空', $('#pwd'));
         return;
     }
     if (data.pwd.length < 8) {
         tips('旧密码不能少于8位', $('#pwd'));
         return;
     }

     if (data.newpwd.length < 8) {
         tips('新密码长度不能少于8位', $('#newpwd'));
         return;
     }
     if (data.confirmpwd !== data.newpwd) {
         tips('两次密码输入不一致', $('#confirmpwd'));
         return;
     }
     return true;
 };
 const submit = (url, data) => {
     $('#submitBtn').attr('disabled', 'disabled');
     $.ajax({
         url: url,
         type: 'post',
         dataType: 'json',
         data: data,
         success: function (res) {
             if (res.rel == 1) {
                 msg(res.msg, function () {
                 window.location.reload();
                     
                 });
             } else {
                 msg(res.msg);
                 $('#submitBtn').removeAttr('disabled');
             }
         }
     });
 };

 export {
     editPwd,loginOut
 }